<?php
declare(strict_types=1);
namespace App\Controller\Admin\V1\Bi;

use App\Controller\Admin\AdminController;
use Hyperf\DbConnection\Db;

class CddController extends AdminController{
    public function thread(){
        $categories = Db::connection('cddlife')->table('thread_category')->whereNull('deleted_at')
            ->orderBy('id','desc')->get()->keyBy('id');
        $positions = Db::connection('cddlife')->table('position_category')->whereNull('deleted_at')
            ->orderBy('id','desc')->get()->keyBy('id');
        $result=Db::connection('cddlife')->table('thread')->whereNull('deleted_at')
        ->orderBy('id','desc')->take(20)->get();
        if($result->count()){
            foreach ($result as &$item){
                $item->files =Db::connection('cddlife')->table('thread_file')->whereNull('deleted_at')
                    ->where('thread_id',$item->id)->take(4)->get();
                $item->category_name = isset($categories[$item->category_id])?$categories[$item->category_id]->name:null;
                $item->position_category_name = isset($positions[$item->position_category_id])?$positions[$item->position_category_id]->name:null;
            }
        }
        return $this->response->success($result);
    }

    public function threadReply(){
        $categories = Db::connection('cddlife')->table('thread_category')->whereNull('deleted_at')
            ->orderBy('id','desc')->get()->keyBy('id');
        $positions = Db::connection('cddlife')->table('position_category')->whereNull('deleted_at')
            ->orderBy('id','desc')->get()->keyBy('id');
        $result=Db::connection('cddlife')->table('thread')->whereNull('deleted_at')
            ->orderBy('id','desc')->where('status',2)->take(30)->get();
        if($result->count()){
            foreach ($result as &$item){
                $item->files =Db::connection('cddlife')->table('thread_file')->whereNull('deleted_at')
                    ->where('thread_id',$item->id)->take(4)->get();
                $accept = Db::connection('cddlife')->table('thread_reply')->whereNull('deleted_at')
                    ->where('thread_id',$item->id)->first();
                $item->accept = $accept;
                $item->category_name = isset($categories[$item->category_id])?$categories[$item->category_id]->name:null;
                $item->position_category_name = isset($positions[$item->position_category_id])?$positions[$item->position_category_id]->name:null;
            }
        }
        return $this->response->success($result);
    }

    public function suggest(){
        $result=Db::connection('cddlife')->table('thread')->whereNull('deleted_at')
            ->orderBy('id','desc')->whereNotNull('suggest')->take(40)->get();
        if($result->count()){
            foreach ($result as &$item){
               $item->wechat=Db::connection('cddlife')->table('user_wechat')->where('user_id',$item->user_id)->first();
            }
        }
        return $this->response->success($result);
    }

    public function wechat(){
        $result = Db::connection('cddlife')->table('wechat_data_analysis')
            ->orderBy('id','asc')->get();
        $day_result = $result[0];
        $week_result = $result[1];
        $month_result = $result[2];
        $province_data=[];
        $ages_data=[];
        $day_visit_uv = json_decode($day_result->visit_uv,true);
        $day_visit_uv_new = json_decode($day_result->visit_uv_new,true);
        $week_visit_uv = json_decode($week_result->visit_uv,true);
        $week_visit_uv_new = json_decode($week_result->visit_uv_new,true);
        $month_visit_uv = json_decode($month_result->visit_uv,true);
        $month_visit_uv_new = json_decode($month_result->visit_uv_new,true);
        $sum_new_day_province = collect($day_visit_uv_new['province'])->sum('value');
        foreach ($day_visit_uv_new['province'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_new_day_province*100).'%';
        }
        $sum_new_week_province = collect($week_visit_uv_new['province'])->sum('value');
        foreach ($week_visit_uv_new['province'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_new_week_province*100).'%';
        }
        $sum_new_month_province = collect($month_visit_uv_new['province'])->sum('value');
        foreach ($month_visit_uv_new['province'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_new_month_province*100).'%';
        }
        $sum_day_province = collect($day_visit_uv['province'])->sum('value');
        foreach ($day_visit_uv['province'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_day_province*100).'%';
        }
        $sum_week_province = collect($week_visit_uv['province'])->sum('value');
        foreach ($week_visit_uv['province'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_week_province*100).'%';
        }
        $sum_month_province = collect($month_visit_uv['province'])->sum('value');
        foreach ($month_visit_uv['province'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_month_province*100).'%';
        }
        $province_data['visit_uv_new']['day'] = $day_visit_uv_new['province'];
        $province_data['visit_uv_new']['week'] = $week_visit_uv_new['province'];
        $province_data['visit_uv_new']['month'] = $month_visit_uv_new['province'];
        $province_data['visit_uv']['day'] = $day_visit_uv['province'];
        $province_data['visit_uv']['week'] = $week_visit_uv['province'];
        $province_data['visit_uv']['month'] = $month_visit_uv['province'];
        foreach($day_visit_uv['province'] as $key=>$item){
            $province_data['all_visit_uv']['day'][$key]=[
                'id'=>$item['id'],
                'name'=>$item['name'],
                'value' => $item['value']+$day_visit_uv_new['province'][$key]['value'],
            ];
        }
        foreach($week_visit_uv['province'] as $key=>$item){
            $province_data['all_visit_uv']['week'][$key]=[
                'id'=>$item['id'],
                'name'=>$item['name'],
                'value' => $item['value']+$week_visit_uv_new['province'][$key]['value']
            ];
        }
        foreach($month_visit_uv['province'] as $key=>$item){
            $province_data['all_visit_uv']['month'][$key]=[
                'id'=>$item['id'],
                'name'=>$item['name'],
                'value' => $item['value']+$month_visit_uv_new['province'][$key]['value']
            ];
        }
        $sum_all_day_province = collect($province_data['all_visit_uv']['day'])->sum('value');
        foreach ($province_data['all_visit_uv']['day'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_all_day_province*100).'%';
        }
        $sum_all_week_province = collect($province_data['all_visit_uv']['week'])->sum('value');
        foreach ($province_data['all_visit_uv']['week'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_all_week_province*100).'%';
        }
        $sum_all_month_province = collect($province_data['all_visit_uv']['month'])->sum('value');
        foreach ($province_data['all_visit_uv']['month'] as &$item){
            $item['ratio'] = sprintf('%.2f',$item['value']/$sum_all_month_province*100).'%';
        }
        $ages_data['visit_uv_new']['day'] = $day_visit_uv_new['ages'];
        $ages_data['visit_uv_new']['week'] = $week_visit_uv_new['ages'];
        $ages_data['visit_uv_new']['month'] = $month_visit_uv_new['ages'];
        $ages_data['visit_uv']['day'] = $day_visit_uv['ages'];
        $ages_data['visit_uv']['week'] = $week_visit_uv['ages'];
        $ages_data['visit_uv']['month'] = $month_visit_uv['ages'];
        foreach($day_visit_uv['ages'] as $key=>$item){
            $ages_data['all_visit_uv']['day'][$key]=[
                'id'=>$item['id'],
                'name'=>$item['name'],
                'value' => $item['value']+$day_visit_uv_new['ages'][$key]['value']
            ];
        }
        foreach($week_visit_uv['ages'] as $key=>$item){
            $ages_data['all_visit_uv']['week'][$key]=[
                'id'=>$item['id'],
                'name'=>$item['name'],
                'value' => $item['value']+$week_visit_uv_new['ages'][$key]['value']
            ];
        }
        foreach($month_visit_uv['ages'] as $key=>$item){
            $ages_data['all_visit_uv']['month'][$key]=[
                'id'=>$item['id'],
                'name'=>$item['name'],
                'value' => $item['value']+$month_visit_uv_new['ages'][$key]['value']
            ];
        }
        return $this->response->success([
            'provinces' => $province_data,
            'ages' => $ages_data
        ]);
    }

    public function waqc(){
        $http = new \GuzzleHttp\Client();
        $params = [

        ];
        try{
            $response = $http->post('http://bi.binggo.me/api/v1/bi/data', [
                'form_params' => []
            ]);
            $return = json_decode((string)$response->getBody(), true);
            return  $this->response->success($return['datas']);
        }catch(\Exception $e){
            return $this->response->success([]);
        }
    }
}